/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows.Controls;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PagePaymentGatewayImp : PageImpBaseClass
    {
        ComboBox cbProcType;
        Button bDeleteTestAccounts;
        Label labelActive;
        DbLinkedCheckBox cbActive;

        /// <summary>
        /// Constructor
        /// </summary>
        public PagePaymentGatewayImp(UIElementCollection stackPanControlsChild,
            DbLinkedTextBox tbMerchantId,
            DbLinkedTextBox tbPassword,
            ComboBox cbType,
            Button bDeleteTestAccounts,
            Label labelActive,
            DbLinkedCheckBox cbActive,
            GroupBox elementGroupBox)
        {

            this.ButtonNumber = "14";
            this.ButtonLabelText = "Credit Configuration";

            mNewGroupIsDisabled = true;
            mDeleteGroupIsDisabled = true;
            mNewEntityIsDisabled = true;
            mDeleteEntityIsDisabled = true;

            this.labelActive = labelActive;
            this.cbActive = cbActive;

            this.bDeleteTestAccounts = bDeleteTestAccounts;
            bDeleteTestAccounts.Visibility = System.Windows.Visibility.Hidden;
            bDeleteTestAccounts.Click += new System.Windows.RoutedEventHandler(bDeleteTestAccounts_Click);
            RegisterSaveMeGroupBoxes(null, elementGroupBox);


            this.cbProcType = cbType;

            ///Register the controls and lock the form.
            RegisterUiElementsList(stackPanControlsChild);

            ///Add all text boxes to the list.
            mTextBoxesList.Add(tbMerchantId);
            mTextBoxesList.Add(tbPassword);


            ///Add the comboboxes.
            ListofComboboxes.Add(cbProcType);


            mEntityAddDeleteLoggingEnabled = true;
        }

        void bDeleteTestAccounts_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                List<PROCESSOR> inactiveProcs = Context.PROCESSORS.Where(p => p.IsSelected == false).ToList();
                if (inactiveProcs != null)
                {
                    foreach (PROCESSOR p in inactiveProcs)
                    {
                        Context.DeleteObject(p);
                    }
                    Context.SaveChanges();

                    bDeleteTestAccounts.Visibility = System.Windows.Visibility.Hidden;
                    labelActive.Visibility = System.Windows.Visibility.Hidden;
                    cbActive.Visibility = System.Windows.Visibility.Hidden;
                    UpdateUsersEntitySource();
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            try
            {
                var q = from PC in Context.PROCESSORS select PC;
                if (q.Any())
                {
                    return new List<EntityObject>(q.ToList());
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            PROCESSOR selected = cbProcType.SelectedItem as PROCESSOR;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void EditStarted()
        {
            try
            {
                var q = from PROC in Context.PROCESSORS select PROC;
                if (!q.Any())
                {
                    PROCESSOR mercLive = new PROCESSOR();
                    mercLive.MerchantId = "595901";
                    mercLive.Password = "N/A";
                    mercLive.ProcessorType = "Live Mercury";
                    mercLive.CardType = "MERCCREDIT";
                    mercLive.EP1 = "x1.mercurypay.com";
                    mercLive.EP2 = "b2.backuppay.com";
                    Context.PROCESSORS.AddObject(mercLive);

                    PROCESSOR noProcessor = new PROCESSOR();
                    noProcessor.MerchantId = "N/A";
                    noProcessor.Password = "N/A";
                    noProcessor.ProcessorType = "No Processor";
                    noProcessor.CardType = "NONE";
                    noProcessor.EP1 = "";
                    noProcessor.EP2 = "";
                    Context.PROCESSORS.AddObject(noProcessor);

                    PROCESSOR mercGift = new PROCESSOR();
                    mercGift.MerchantId = "595901";
                    mercGift.Password = "N/A";
                    mercGift.ProcessorType = "Live Mercury Gift";
                    mercGift.CardType = "MERCGIFT";
                    mercGift.EP1 = "g1.mercurypay.com";
                    mercGift.EP2 = "g2.backuppay.com";
                    Context.PROCESSORS.AddObject(mercGift);

                    PROCESSOR mercTest = new PROCESSOR();
                    mercTest.MerchantId = "595901";
                    mercTest.Password = "N/A";
                    mercTest.ProcessorType = "Test Mercury";
                    mercTest.CardType = "MERCCREDIT";
                    mercTest.EP1 = "x1.mercurydev.net";
                    mercTest.EP2 = "x2.mercurydev.net";
                    mercTest.IsSelected = true;
                    Context.PROCESSORS.AddObject(mercTest);

                    PROCESSOR mercGiftTest = new PROCESSOR();
                    mercGiftTest.MerchantId = "595901";
                    mercGiftTest.Password = "N/A";
                    mercGiftTest.ProcessorType = "Test Mercury Gift";
                    mercGiftTest.CardType = "MERCGIFT";
                    mercGiftTest.EP1 = "g1.mercurydev.net";
                    mercGiftTest.EP2 = "g2.mercurydev.net";
                    mercGiftTest.IsSelected = true;
                    Context.PROCESSORS.AddObject(mercGiftTest);

                    PROCESSOR authNetLive = new PROCESSOR();
                    authNetLive.MerchantId = "7G6Ux97Sqyst";
                    authNetLive.Password = "76kf798S7RtqEyxQ";
                    authNetLive.ProcessorType = "Live Authorize.net";
                    authNetLive.CardType = "AUTHCREDIT";
                    Context.PROCESSORS.AddObject(authNetLive);
                    Context.SaveChanges();

                    PROCESSOR authNetTest = new PROCESSOR();
                    authNetTest.MerchantId = "7G6Ux97Sqyst";
                    authNetTest.Password = "76kf798S7RtqEyxQ";
                    authNetTest.ProcessorType = "Test Authorize.net";
                    authNetTest.CardType = "AUTHCREDIT";
                    Context.PROCESSORS.AddObject(authNetTest);

                    PROCESSOR mercVer1000PIN = new PROCESSOR();
                    mercVer1000PIN.MerchantId = "595901";
                    mercVer1000PIN.Password = "N/A";
                    mercVer1000PIN.ProcessorType = "Mercury VER1000 PIN";
                    mercVer1000PIN.CardType = "MERCVER1000PIN";
                    mercVer1000PIN.EP1 = "x1.mercurypay.com";
                    mercVer1000PIN.EP2 = "b2.backuppay.com";
                    mercVer1000PIN.ExtDevicePrompt = "OCEANSIDE POS";
                    Context.PROCESSORS.AddObject(mercVer1000PIN);

                    Context.SaveChanges();
                    UpdateUsersEntitySource();

                    bDeleteTestAccounts.Visibility = System.Windows.Visibility.Visible;
                    labelActive.Visibility = System.Windows.Visibility.Visible;
                    cbActive.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    List<PROCESSOR> inactiveProcs = Context.PROCESSORS.Where(p => p.IsSelected == false).ToList();
                    if (inactiveProcs != null && inactiveProcs.Count != 0)
                    {
                        bDeleteTestAccounts.Visibility = System.Windows.Visibility.Visible;
                        labelActive.Visibility = System.Windows.Visibility.Visible;
                        cbActive.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        bDeleteTestAccounts.Visibility = System.Windows.Visibility.Hidden;
                        labelActive.Visibility = System.Windows.Visibility.Hidden;
                        cbActive.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            try
            {
                ///Create the query object.
                var query = from PROCESSOR in Context.PROCESSORS
                            orderby PROCESSOR.ProcessorType
                            select PROCESSOR;

                if (query.Any())
                {
                    List<PROCESSOR> list = query.ToList();
                    cbProcType.ItemsSource = list;
                    if (list != null && list.Count != 0)
                    {
                        cbProcType.SelectedItem = list.First();
                    }
                    else
                    {
                        cbProcType.ItemsSource = null;
                    }
                }
                else
                {
                    cbProcType.ItemsSource = null;
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return cbProcType;
        }
    }
}
